package com.zzyy.study.config;

import cn.hutool.core.util.IdUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @auther zzyy
 * @create 2024-01-27 17:47
 * JUC里面默认线程池类，原生版
 */
@Configuration
public class ExecutorServiceConfig
{
    @Bean
    public ExecutorService executorService()
    {
        int coreSize = Runtime.getRuntime().availableProcessors();

        return new ThreadPoolExecutor(
                coreSize * 2 + 1,
                coreSize * 5,
                2L,
                TimeUnit.MINUTES,
                new LinkedBlockingQueue<>(100),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }


    /*public static void main(String[] args) throws Exception
    {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();


        new Thread(() -> {
            try {
                blockingQueue.put(IdUtil.simpleUUID());
                blockingQueue.put(IdUtil.simpleUUID());
                blockingQueue.put(IdUtil.simpleUUID());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t1").start();

        new Thread(() -> {
            try {
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println(Thread.currentThread().getName()+"\t"+blockingQueue.take());
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println(Thread.currentThread().getName()+"\t"+blockingQueue.take());
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println(Thread.currentThread().getName()+"\t"+blockingQueue.take());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t2").start();

    }*/
}
